package com.mqtt.demo.callback;

import com.mqtt.demo.mqtt.MyMqttClient;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;

/**
 * @ClassName MessageCallback
 * @Description mqtt回调
 * @Author zhengmw
 * @Data 2024/7/25 18:20
 */
@Slf4j
@Component
public class MessageCallback implements MqttCallback {
    @Resource
    private MyMqttClient myMqttClient;

    @Override
    public void connectionLost(Throwable throwable) {

        try {
            myMqttClient.connectAndSubscribe();
            log.info("******** 断开链接，尝试重连");

        }catch (MqttException e) {
            log.error("******** mqtt连接断开，重连失败");
        }
    }

    /**
     * @Description 消息接收
     *
     * @Param s
     * @Param mqttMessage
     * @Return void
     * @Author zhengmw
     * @Date 2024/7/25 19:17
     */
    @Override
    public void messageArrived(String topic, MqttMessage mqttMessage) throws Exception {
        log.info("**********接受消息主题：{}，接受的消息：{}", topic, mqttMessage);

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {
        log.info("**********消息处理完成");
    }























}
